import pickle
import socket
import threading

# We'll pickle a list of numbers:
#someList = [ 1, 2, 7, 9, 0 ]
# actually lets make it look just like a game state
state = ( [(1234, 123, 360, 1, 0), (1234, 123, 360, 1, 0), (1234, 123, 360, 1, 0), (1234, 123, 360, 1, 0), (1234, 123, 360, 1, 0), (1234, 123, 360, 1, 0)], [("red",1234,123,360), ("red",1234,123,360),("red",1234,123,360),("red",1234,123,360),("red",1234,123,360),("red",1234,123,360),("red",1234,123,360),("red",1234,123,360)], [(1234,1234,99), (1234,1234,99), (1234,1234,99), (1234,1234,99)], [(99,99), (99,99),(99,99),(99,99),(99,99),(99,99),],5)

#pickledList = pickle.dumps ( someList )
pickledList = pickle.dumps ( state )

# Our thread class:
class ClientThread ( threading.Thread ):

   # Override Thread's __init__ method to accept the parameters needed:
   def __init__ ( self, channel, details ):

      self.channel = channel
      self.details = details
      threading.Thread.__init__ ( self )

   def run ( self ):

      print 'Received connection:', self.details [ 0 ]
      self.channel.send ( pickledList )
      for x in xrange ( 10 ):
         print self.channel.recv ( 1024 )
      self.channel.close()
      print 'Closed connection:', self.details [ 0 ]

# Set up the server:
server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
server.bind ( ( '', 2727 ) )
server.listen ( 5 )

# Have the server serve "forever":
while True:
   channel, details = server.accept()
   ClientThread ( channel, details ).start()
